﻿using System;
using System.Collections;
using System.Collections.Generic;
using AudioStudio;
using com.youzu.warh.protocol;
using LogSystem;
using UnityEngine;
using WorldMapBase.Layer;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Gameplay.Liberty.Pool;
using Yoozoo.Gameplay.Liberty.World;
using Yoozoo.Mars.Got;
using Random = UnityEngine.Random;

namespace Yoozoo.Gameplay.Liberty.AI
{
    public class BodyRenderController : MonoBehaviour
    {
        [Serializable]
        public struct BoneData
        {
            public string name;
            public Transform bone;
        }
        
        // 常驻SkinnedMeshRenderer列表，优化GC
        public static List<SkinnedMeshRenderer> SkinnedMeshRenderers = new List<SkinnedMeshRenderer>(10);
        
        // 骨骼根节点
        public Transform rootBone;
        // 骨骼列表
        public List<BoneData> bones = new List<BoneData>();        
        // 骨骼映射
        private Dictionary<string, Transform> bonesMap = new Dictionary<string, Transform>();

        void Awake()
        {
            InitBonesMap();
        }
        
        void OnDestroy()
        {
            rootBone = null;
        }
        
        public void InitBonesMap()
        {
            bonesMap.Clear();
            foreach (var boneData in bones)
            {
                bonesMap[boneData.name] = boneData.bone;
            }
        }

        /// <summary>
        /// 添加身体部件
        /// </summary>
        /// <param name="bodyPart"></param>
        public void AddBodyPart(Transform bodyPart)
        {
            if (!rootBone)
            {
                return;
            }
            
            bodyPart.parent = transform;
            bodyPart.localPosition = Vector3.zero;
            bodyPart.localRotation = Quaternion.identity;
            
            bodyPart.GetComponentsInChildren<SkinnedMeshRenderer>(SkinnedMeshRenderers);
            
            foreach (var skinnedMeshRenderer in SkinnedMeshRenderers)
            {
                Transform[] bones = new Transform[skinnedMeshRenderer.bones.Length];
                for (int i = 0; i < bones.Length; ++i)
                {
                    bones[i] = FindBoneNode(skinnedMeshRenderer.bones[i].name);
                }
                skinnedMeshRenderer.rootBone = rootBone;
                skinnedMeshRenderer.bones = bones;
            }
        }
        
        /// <summary>
        /// 从LibertyPool中添加身体部件
        /// </summary>
        /// <param name="bodyPartId"></param>
        public void AddBodyPartFromLibertyPool(int bodyPartId)
        {
            Transform bodyPart = LibertyPoolMgr.GetInstance().GetEffectObject(bodyPartId);
            if (bodyPart != null)
            {
                AddBodyPart(bodyPart);
            }
        }
        
        private Transform FindBoneNode(string name)
        {
            Transform bone = null;
            if (!bonesMap.TryGetValue(name, out bone))
            {
                UnityEngine.Debug.LogError("找不到骨骼: " + name);
            }

            return bone;
        }
        
    }
}